package com.gmail.hongyz.myfuse.demo.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.gmail.hongyz.myfuse.dataengine.Dataset;
import com.gmail.hongyz.myfuse.dataengine.JsonRequest;
import com.gmail.hongyz.myfuse.dataengine.JsonResponse;
import com.gmail.hongyz.myfuse.dataengine.Record;
import com.gmail.hongyz.myfuse.service.UserService;

public class DemoController extends MultiActionController {

	private static final Log log = LogFactory.getLog(DemoController.class);
	
	private UserService userService;
	
	public ModelAndView init(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		if (log.isDebugEnabled()) {
            log.debug("entering 'handleRequest' method...");
        }
		return new ModelAndView("user");
	}
	
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public void callJson(JsonRequest request, JsonResponse response) throws Exception {
	
		if (log.isDebugEnabled()) {
            log.debug("entering 'handleRequest' method...");
        }
		try {
		
		String userId = request.getParameter("userId");
		String name = request.getParameter("name");
//			String password = json.getString("password");
		log.debug("name == null: " + name == null);
		log.debug("name: " + name);
		String email = request.getParameter("email");
		if (email.endsWith(".com")) {
			log.debug("email end with .com");
		}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		Dataset dataset = request.getDataset("001");
		log.debug("record02.name: " + dataset.getRecord(1).get("name"));
		response.setParameter("saySomething", "hello,world");
		
		Dataset dataset2 = new Dataset();
		Record record = new Record();
		record.set("xxx", "000");
		dataset2.addRecord(record);
		response.setDataset("ds1", dataset2);
	}
}
